package com.tengfei.api.course;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tengfei.common.annotation.Log;
import com.tengfei.common.annotation.RepeatSubmit;
import com.tengfei.common.core.controller.BaseController;
import com.tengfei.common.core.domain.PageQuery;
import com.tengfei.common.core.domain.R;
import com.tengfei.common.core.page.TableDataInfo;
import com.tengfei.common.core.validate.AddGroup;
import com.tengfei.common.core.validate.EditGroup;
import com.tengfei.common.enums.BusinessType;
import com.tengfei.main.course.domain.bo.ChapterBo;
import com.tengfei.main.course.domain.bo.ChapterResBo;
import com.tengfei.main.course.domain.bo.ChapterVideoBo;
import com.tengfei.main.course.domain.bo.VideoQuBo;
import com.tengfei.main.course.domain.vo.ChapterVideoVo;
import com.tengfei.main.course.domain.vo.ChapterVo;
import com.tengfei.main.course.domain.vo.ResVo;
import com.tengfei.main.course.domain.vo.VideoQuVo;
import com.tengfei.main.course.service.IChapterService;
import com.tengfei.main.question.domain.vo.ObjectiveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 章节Controller
 *
 * @author tengfei
 * @date 2022-07-27
 */
@Validated
@Api(value = "章节控制器", tags = {"章节管理"})
@RestController
@RequestMapping("/course/chapter")
public class ChapterController extends BaseController {

    @Resource
    private IChapterService iChapterService;

    /**
     * 查询章节资源列表
     */
    @ApiOperation("查询章节资源列表")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/res/list")
    public TableDataInfo<ResVo> resList(@ApiParam("章节id") @NotNull(message = "章节id不能为空")
                                        @RequestParam("chapterId") Long chapterId, PageQuery pageQuery) {
        return iChapterService.queryResList(chapterId, pageQuery);
    }

    /**
     * 获取资源详细信息
     */
    @ApiOperation("获取资源详细信息")
    @SaCheckPermission("course:chapter:query")
    @GetMapping("/res/{resId}")
    public R<ResVo> resInfo(@ApiParam("资源id")
                            @NotNull(message = "资源id不能为空")
                            @PathVariable("resId") Long resId) {
        return R.ok(iChapterService.queryResById(resId));
    }

    /**
     * 批量删除章节资源
     */
    @ApiOperation("批量删除章节资源")
    @SaCheckPermission("course:chapter:remove")
    @Log(title = "批量删除章节资源", businessType = BusinessType.DELETE)
    @DeleteMapping("/res")
    public R<Void> removeRes(@ApiParam("id数组")
                             @NotEmpty(message = "id数组为空")
                             @RequestParam("ids[]") List<Long> ids) {
        return toAjax(iChapterService.deleteResByChapterIds(ids) ? 1 : 0);
    }

    /**
     * 修改章节资源
     */
    @ApiOperation("修改章节资源")
    @SaCheckPermission("course:chapter:edit")
    @Log(title = "修改章节资源", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/res")
    public R<Void> editRes(@ApiParam("资源id") @NotNull(message = "资源id不能为空")
                           @RequestParam("resId") Long resId,
                           @ApiParam("资源名称") @NotNull(message = "资源名称不能为空")
                           @RequestParam("resName") String resName) {
        return toAjax(iChapterService.editRes(resId, resName));
    }

    /**
     * 批量新增章节资源
     */
    @ApiOperation("批量新增章节资源")
    @SaCheckPermission("course:chapter:add")
    @Log(title = "批量新增章节资源", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/res")
    public R<Void> addRes(@Validated(AddGroup.class) @RequestBody List<ChapterResBo> bos) {
        return toAjax(iChapterService.addRes(bos));
    }

    /**
     * 查询章节视频列表
     */
    @ApiOperation("查询章节视频列表")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/video/list")
    public TableDataInfo<ChapterVideoVo> videoList(@ApiParam("章节id") @NotNull(message = "章节id不能为空") Long chapterId,
                                                   PageQuery pageQuery) {
        return iChapterService.queryVideoList(chapterId, pageQuery);
    }

    /**
     * 查询章节视频详情
     */
    @ApiOperation("查询章节视频详情")
    @SaCheckPermission("course:chapter:query")
    @GetMapping("/video")
    public R<ChapterVideoVo> queryVideo(@ApiParam("章节id") @NotNull(message = "章节id不能为空") Long chapterId,
                                        @ApiParam("视频id") @NotNull(message = "视频id不能为空") Long videoId) {
        return R.ok(iChapterService.queryVideo(chapterId, videoId));
    }


    /**
     * 批量删除章节视频
     */
    @ApiOperation("批量删除章节视频")
    @SaCheckPermission("course:chapter:remove")
    @Log(title = "批量删除章节视频", businessType = BusinessType.DELETE)
    @DeleteMapping("/video")
    public R<Void> removeVideo(@ApiParam("id数组")
                               @NotEmpty(message = "id数组为空")
                               @RequestParam("ids[]") List<Long> ids) {
        return toAjax(iChapterService.deleteVideoByChapterIds(ids) ? 1 : 0);
    }

    /**
     * 修改章节视频
     */
    @ApiOperation("修改章节视频")
    @SaCheckPermission("course:chapter:edit")
    @Log(title = "修改章节视频", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PutMapping("/video")
    public R<Void> editVideo(@Validated(EditGroup.class) @RequestBody ChapterVideoBo bo) {
        return toAjax(iChapterService.editVideo(bo));
    }

    /**
     * 新增章节视频（可批量新增）
     */
    @ApiOperation("新增章节视频")
    @SaCheckPermission("course:chapter:add")
    @Log(title = "新增章节视频", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/video")
    public R<Void> addVideo(@Validated(AddGroup.class) @RequestBody List<ChapterVideoBo> bos) {
        return toAjax(iChapterService.addVideo(bos));
    }

    /**
     * 查询章节视频已绑定题目列表
     */
    @ApiOperation("查询章节已绑定题目列表")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/video/bindQu/list")
    public R<List<VideoQuVo>> queryVideoBindQuList(@ApiParam("id") @NotNull(message = "id不能为空") Long id) {
        return R.ok(iChapterService.queryVideoBindQuList(id));
    }

    /**
     * 章节视频解绑题目
     */
    @ApiOperation("章节视频解绑题目")
    @SaCheckPermission("course:chapter:edit")
    @PutMapping("/video/qu/del")
    public R<Void> videoDelQu(@ApiParam("id") @NotNull(message = "id不能为空") @RequestParam("id") Long id,
                              @ApiParam("objId") @NotNull(message = "题目id不能为空") @RequestParam("objId") Long objId) {
        return toAjax(iChapterService.videoDelQu(id, objId));
    }

    /**
     * 查询章节视频未绑定题目列表
     */
    @ApiOperation("查询章节未绑定题目列表")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/video/unBindQu/list")
    public R<TableDataInfo<ObjectiveVo>> queryVideoUnBindQuList(VideoQuBo bo, PageQuery pageQuery) {
        return R.ok(iChapterService.queryVideoUnBindQuList(bo, pageQuery));
    }

    /**
     * 章节视频绑定题目
     */
    @ApiOperation("章节视频绑定题目")
    @SaCheckPermission("course:chapter:edit")
    @PutMapping("/video/qu/add")
    public R<Void> videoAddQu(@ApiParam("id") @NotNull(message = "id不能为空") @RequestParam("id") Long id,
                              @ApiParam("objId") @NotNull(message = "题目id不能为空") @RequestParam("objId") Long objId,
                              @ApiParam("second") @NotNull(message = "第多少秒不能为空") @RequestParam("second") Integer second) {
        return toAjax(iChapterService.videoAddQu(id, objId,second));
    }


    /**
     * 查询章节列表通过课程id
     */
    @ApiOperation("查询章节列表")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/list")
    public TableDataInfo<ChapterVo> list(@ApiParam("课程id") @NotNull(message = "课程id不能为空") Long courseId, PageQuery pageQuery) {
        return iChapterService.queryPageList(courseId, pageQuery);
    }

    /**
     * 查询章节简易列表信息通过课程id
     */
    @ApiOperation("查询章节简易列表信息通过课程id")
    @SaCheckPermission("course:chapter:list")
    @GetMapping("/listSimple")
    public R<List<Map<String, Object>>> listSimple(@ApiParam("课程id") @NotNull(message = "课程id不能为空") Long courseId) {
        return R.ok(iChapterService.querySimplePageList(courseId));
    }

    /**
     * 获取章节详细信息
     */
    @ApiOperation("获取章节详细信息")
    @SaCheckPermission("course:chapter:query")
    @GetMapping("/{chapterId}")
    public R<ChapterVo> getInfo(@ApiParam("章节id")
                                @NotNull(message = "章节id不能为空")
                                @PathVariable("chapterId") Long chapterId) {
        return R.ok(iChapterService.queryById(chapterId));
    }

    /**
     * 新增章节
     */
    @ApiOperation("新增章节")
    @SaCheckPermission("course:chapter:add")
    @Log(title = "新增章节", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChapterBo bo) {
        return toAjax(iChapterService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改章节
     */
    @ApiOperation("修改章节")
    @SaCheckPermission("course:chapter:edit")
    @Log(title = "修改章节", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChapterBo bo) {
        return toAjax(iChapterService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 批量删除章节
     */
    @ApiOperation("批量删除章节")
    @SaCheckPermission("course:chapter:remove")
    @Log(title = "批量删除章节", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public R<Void> remove(@ApiParam("章节id数组")
                          @NotEmpty(message = "章节id数组不能为空")
                          @RequestParam("chapterIds[]") List<Long> ids) {
        return toAjax(iChapterService.deleteWithValidByIds(ids) ? 1 : 0);
    }
}
